﻿CREATE PROCEDURE ProductReview_CreateNew
	@body varchar (8000),
	@userEmail varchar(50),
	@userName varchar(50),
	@rating int = 0,
	@subject varchar(50),
	@productId int = null
AS
	SET NOCOUNT ON
	insert into [ProductReviews] (Body, UserEmail, UserName, Rating, Subject, ProductId)
	values (@body, @userEmail, @userName, @rating, @subject, @productId)
	
	declare @averageRating float
	select @averageRating = avg (Rating) from ProductReviews where ProductId = @productId
	
	update Products set
		Rating = @averageRating
	where ProductId = @productId
	RETURN


